package com.huluobo.a2204kotlinshopping.category

import android.content.Intent
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.adapter.CategoryChildAdapter
import com.huluobo.a2204kotlinshopping.adapter.CategoryGoodsListAdapter
import com.huluobo.a2204kotlinshopping.adapter.CategoryParentAdapter
import com.huluobo.a2204kotlinshopping.databinding.FragmentCategoryBinding
import com.huluobo.a2204kotlinshopping.detail.DetailActivity
import com.huluobo.base.bean.CategoryBean
import com.huluobo.base.bean.CategoryGoodsListBean
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseMVVMFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CategoryFragment : BaseMVVMFragment<FragmentCategoryBinding, CategoryViewModel>() {
    //一级列表数据源和adapter
    private val parentList = mutableListOf<CategoryBean>()
    private lateinit var parentAdapter: CategoryParentAdapter

    //二级列表数据源和adapter
    private val childList = mutableListOf<CategoryBean>()
    private lateinit var childAdapter: CategoryChildAdapter

    //三级列表数据源和adapter
    private val goodsList = mutableListOf<CategoryGoodsListBean>()
    private lateinit var goodsAdapter: CategoryGoodsListAdapter

    //刷新时需要使用的page,下拉刷新时初始化,上拉加载时++
    private var currentPage = 1

    //刷新时,需要使用的三级列表请求数据的id
    private var childId = 0

    override fun getLayoutId(): Int = R.layout.fragment_category

    override fun initData() {
        viewModel.getCategoryList()

        viewModel.getCategoryListSuccess.observe(this) {
            if (it.code == 200) {
                parentList.clear()
                parentList.addAll(it.data)
                parentAdapter.notifyDataSetChanged()

                //进入页面初始化二级列表数据
                val data = parentList[0].data
                if (data != null) {
                    childList.clear()
                    childList.addAll(data)
                    childAdapter.notifyDataSetChanged()
                    if (childList.size != 0) {
                        val categoryId = childList[0].id
                        viewModel.getCategoryGoodsList(categoryId, currentPage, 10)
                    }
                }
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getCategoryListFailed.observe(this) {
            Log.i("TagA", "获取分类列表失败:$it")
        }

        viewModel.getCategoryGoodsListSuccess.observe(this) {
            dataBinding.categorySrl.finishLoadMore()
            dataBinding.categorySrl.finishRefresh()
            if (it.code == 200) {
                if (currentPage == 1) {
                    goodsList.clear()
                }
                goodsList.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getCategoryGoodsListFailed.observe(this) {
            Log.i("TagA", "获取三级列表失败:$it")
        }
    }

    override fun initView() {
        dataBinding.categoryParentRv.layoutManager = LinearLayoutManager(requireContext())
        parentAdapter = CategoryParentAdapter(parentList)
        dataBinding.categoryParentRv.adapter = parentAdapter

        //一级列表的点击事件,切换二级列表
        parentAdapter.setOnItemClickListener { adapter, view, position ->
            goodsList.clear()
            childId = childList[0].id
            parentAdapter.setSelectItemId(parentList[position].id)
            val data = parentList[position].data
            if (data != null) {
                childList.clear()
                childList.addAll(data)
                childAdapter.notifyDataSetChanged()
                //点击一级列表的同时,需要初始化三级列表
                if (childList.size != 0) {
                    val categoryId = childList[0].id
                    viewModel.getCategoryGoodsList(categoryId, currentPage, 10)
                }
            }
        }

        dataBinding.categoryChildRv.layoutManager =
            GridLayoutManager(requireContext(), 2, RecyclerView.HORIZONTAL, false)
        childAdapter = CategoryChildAdapter(childList)
        dataBinding.categoryChildRv.adapter = childAdapter


        //二级类表的点击事件
        childAdapter.setOnItemClickListener { adapter, view, position ->
            if (childList.size != 0) {
                childId = childList[position].id
                viewModel.getCategoryGoodsList(childList[position].id, currentPage, 10)
            }
        }

        //三级列表初始化
        dataBinding.categoryGoodsListRv.layoutManager = LinearLayoutManager(requireContext())
        goodsAdapter = CategoryGoodsListAdapter(goodsList)
        dataBinding.categoryGoodsListRv.adapter = goodsAdapter

        //初始化刷新框架
        dataBinding.categorySrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage = 1
                viewModel.getCategoryGoodsList(childId, currentPage, 10)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                currentPage += 1
                viewModel.getCategoryGoodsList(childId, currentPage, 10)
            }
        })

        //三级列表的点击事件
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(requireContext(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }
    }

    override fun getViewModel(): Class<CategoryViewModel> {
        return CategoryViewModel::class.java
    }
}